package com.example.consumer;

import com.example.entity.Order;
import com.example.entity.OrderSort;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class OrderAsyncConsumer {

    @RabbitListener(queues = "order.async.queue")
    public String handleOrder(@Payload Order order, Channel channel, Message message) throws IOException {// 注意：不要直接获取 deliveryTag 作为参数
        log.info("order.async.queue");
        // 手动确认消息（deliveryTag：消息唯一标识；multiple：是否批量确认）
        try {
            Thread.sleep(3000L);
            log.info("订单处理成功: {}", order.getId());
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            return "SUCCESS"; // 或者其他响应内容
        } catch (IOException e) {
            log.error("处理订单失败: {}", order.getId(), e);
            return "FAILED";
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }


}